class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        int len = data.size();
        int mid;
        int pos1 = -1, pos2 = -1;
        
        int l = 0, r = len;
        while(l < r) {
            mid = l + (r - l)/2;
            if(data[mid] == k) {
                pos1 = mid;
                r = mid;
            }
            else if(data[mid] < k) l = mid + 1;
            else r = mid;
        }
        if(pos1 == -1) return 0;
        
        l = 0, r = len;
        while(l < r) {
            mid = l + (r - l)/2;
            if(data[mid] == k) {
                pos2 = mid;
                l = mid + 1;
            }
            else if(data[mid] < k) l = mid + 1;
            else r = mid;
        }
        return pos2 - pos1 + 1;
    }
    
};